package com.yidu.hbatsft.action;


import com.yidu.hbatsft.domain.AtsBaseSport;
import com.yidu.hbatsft.service.AtsBaseSportItemService;
import com.yidu.hbatsft.service.AtsBaseSportService;
import com.yidu.utils.Message;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.annotation.MultipartConfig;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @author hp
 */
@MultipartConfig
@Controller
@RequestMapping("/AtsBaseSport")
public class AtsBaseSportController {

    Logger logger = Logger.getLogger(getClass());

    @Resource
    AtsBaseSportService atsBaseSportService;

    @Resource
    AtsBaseSportItemService atsBaseSportItemService;

    @ResponseBody
    @RequestMapping("selected")
    public List<AtsBaseSport> selected(){
        return atsBaseSportService.selectAll();
    }


    /**
     * 根据id删除
     * @param id 项目id
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteSp")
    public Message deleteSp(Integer id){
        Message message = new Message();
        int del = atsBaseSportService.deleteSp(id);
        message.setMsg("操作成功");
        message.setNum(1);
        return message;
    }

    /**
     * 新增与修改
     * @param atsBaseSport 体育项目对象
     * @param ids id
     * @return
     */
    @ResponseBody
    @RequestMapping("intsertUpdate")
    public Message intsertUpdate(AtsBaseSport atsBaseSport,String ids){
        if(atsBaseSport.getSportId()==0){
            Random random = new Random();
            //生成随机数
            int id = random.nextInt(10000000);
            atsBaseSport.setSportId(id);
            atsBaseSportService.insert(atsBaseSport);
            atsBaseSportItemService.xhzj(ids,id);
        }else {
            atsBaseSportService.insertUpdate(atsBaseSport);
            atsBaseSportItemService.xhzj(ids,atsBaseSport.getSportId());
        }
        Message message = new Message();
        message.setMsg("操作成功");
        message.setNum(1);
        return message;
    }

    /**
     * 根据id查询
     * @param id 项目id
     * @return
     */
    @ResponseBody
    @RequestMapping("findId")
    public AtsBaseSport findId(Integer id){
            return atsBaseSportService.findId(id);
    }

    /**
     * 查询所有
     * @param pageNum 页数
     * @param pageSize 总行数
     * @return
     */
    @ResponseBody
    @RequestMapping("selectAll")
    public Map<String,Object> selectAll(@RequestParam("page") Integer pageNum, @RequestParam("limit") Integer pageSize){
        Map<String,Object> map = new HashMap<String, Object>();
        List<AtsBaseSport> list = atsBaseSportService.selectAllPage(pageNum,pageSize);
        List<AtsBaseSport> list1 = atsBaseSportService.selectAll();
        map.put("code", 0);
        map.put("msg", "ok");
        map.put("count", list1.size());
        map.put("data", list);
        return map;
    }

    /**
     * 模糊查询
     * @param pageNum 页数
     * @param pageSize 总行数
     * @return
     */
    @ResponseBody
    @RequestMapping("findName")
    public Map<String,Object> findName(@RequestParam("page") Integer pageNum, @RequestParam("limit") Integer pageSize,String newsTitle){
        Map<String,Object> map = new HashMap<String, Object>();
        List<AtsBaseSport> list1 = atsBaseSportService.findName(newsTitle);
        List<AtsBaseSport> list = atsBaseSportService.findNamePage(newsTitle,pageNum,pageSize);
        map.put("code", 0);
        map.put("msg", "ok");
        map.put("count", list1.size());
        map.put("data", list);
        return map;
    }
}
